package be.scond.cartpool.model;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ShoppingList {

    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
    @Persistent(mappedBy = "shoppingList")
    private List<Order> orders;
    
    @Persistent
    private Group group;
    
    @Persistent
    private Key closedByUserKey; 
    
    @Persistent
    private String closedByUserName; 
    

	@Persistent
    private BigDecimal payedAmount = BigDecimal.ZERO;
    
    @Persistent
	private Date date;
		
	@Persistent
	private boolean closed = false;
	
		
	public ShoppingList(){}
	
	public String toXmlString(){
		String str = "<shoppingList>";
		str += "<key>" + KeyFactory.keyToString(key) + "</key>";
		str += "<closed>" + closed + "</closed>";
		if(closed){
			str += "<closedByUserKey>" + KeyFactory.keyToString(closedByUserKey) + "</closedByUserKey>";			
			str += "<closedByUserName>" + closedByUserName + "</closedByUserName>";			
		}
		NumberFormat form = DecimalFormat.getInstance();        
		str += "<payedAmount>"+form.format(payedAmount)+"</payedAmount>";
		str += "<date>" + date.getTime() + "</date>";
		if(orders.size() > 0){
			str += "<orders>";
			for(Order o : orders){
				str += o.toXmlString();
			}
			str += "</orders>";			
		}		
		str += "</shoppingList>";
		return str;
	}
	
	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}

	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}

	public Key getClosedByUserKey() {
		return closedByUserKey;
	}
	public void setClosedByUserKey(Key closedByUserKey) {
		this.closedByUserKey = closedByUserKey;
	}
	
    public String getClosedByUserName() {
		return closedByUserName;
	}
	public void setClosedByUserName(String closedByUserName) {
		this.closedByUserName = closedByUserName;
	}
	
	public BigDecimal getPayedAmount() {
		return payedAmount;
	}

	public void setPayedAmount(BigDecimal payedAmount) {
		this.payedAmount = payedAmount;
	}

	public boolean isClosed() {
		return closed;
	}
	public void setClosed(boolean closed) {
		this.closed = closed;
	}

	public List<Order> getOrders() {
		return orders;
	}
	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
	
}
